package com.zoran.study.spring.helper;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * MyInvocationHandler
 *
 * @author zouxm
 * @date 15/11/4
 */
public class MyInvocationHandler<T> implements InvocationHandler {

    private T target;

    public MyInvocationHandler(T target) {
        this.target = target;
    }

    public T getProxy() {
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(proxy instanceof Proxy);
        System.out.println("isProxy:" + Proxy.isProxyClass(proxy.getClass()));
        System.out.println("正在执行的方法：" + method);
        System.out.println(Arrays.toString(args));
        System.out.println(proxy.getClass().getSimpleName());
        System.out.println("代理方法执行前");
        Object result = method.invoke(target, args);
        System.out.println("result:"+result);
        System.out.println("代理方法执行后");
        return result;
    }
}
